/*************************************************************
 *Name:           Christopher Alexander
 *Email address:  alexanc2@onid.oregonstate.edu
 *Class name:     CS362-400
 *Assignment:     Team 9 - Project Test whoseTurn()
 *Description:    The program will peform unit testing on the
 *                dominion.c function whoseTurn().
 *************************************************************/

#include "dominion.h"
#include "dominion_helpers.h"
#include <string.h>
#include <stdio.h>
#include <assert.h>
#include "rngs.h"

#define NUM_UNIQUE_CARDS 27

/*Function Prototypes*/
void checkWhoseTurn(int, struct gameState *state);


/*This function will determine whose turn it is in two ways,
 *and then compare the results to make sure they are the same.
 *1)The endTurn() function of dominion.c will be used advance the
 *  state->whoseTurn variable.
 *2)Whose turn it currently is will be tracked manually.
 *Params: currentTurn - Will be used to manually whose turn it is.
 *        state - a pointer to a struct gameState.
 *Return: None.
 */
void checkWhoseTurn(int currentTurn, struct gameState *state) {

   //Check that the initial values to track whose turn are the same.
   assert(currentTurn == state->whoseTurn);

   //Continue to check whoseTurn() until the game is over.
   while(!isGameOver(state)){
      endTurn(state); //This will advance turn to next player.

      //Manually advance turn of currentTurn.
      if (currentTurn == ((state->numPlayers) - 1)) {
         currentTurn = 0;
      }
      else {
         currentTurn++;
      }

      //Check that the values to track whose turn are still the same.
      assert(currentTurn == state->whoseTurn);

      state->supplyCount[(int) (Random() * 27)]--; //Will eventually end the game.
   }
}


int main() {
   int i, n, players, firstTurn, numSeed;
   struct gameState G;
   int k[10] = {adventurer, council_room, feast, gardens, mine,
               remodel, smithy, village, baron, great_hall};

   printf("\nTesting whoseTurn():\n");

   for (n = 0; n < 2000; n++) {

      //Randomly fill struct gameState.
      for (i = 0; i < sizeof(struct gameState); i++){
         ((char*)&G)[i] = (int) (Random() * 256);
      }

      //Randomly set the number of players to be 2, 3, or 4.
      players = (int) (Random() * 3) + 2;

      //Set the seed to use in initializeGame.
      numSeed = (int) (Random() * 1000) + 1;

      initializeGame(players, k, numSeed, &G);

      //Set the player who goes first randomly.
      firstTurn = (int) (Random() * players);
      G.whoseTurn = firstTurn;

      checkWhoseTurn(firstTurn, &G);
   }
   printf("All tests of whoseTurn() passed!\n");
   return 0;
}
